<?php
require('../qcubed.inc.php');
//Add Auth

//Panel
require_once('panels/ComSchoolListPanel.class.php');
require_once('panels/ComSchoolEditPanel.class.php');
require_once('panels/ComSchoolInfoPanel.class.php');

require_once('panels/ComNoteListPanel.class.php');
require_once('panels/ComNoteEditPanel.class.php');
require_once('panels/ComNoteInfoPanel.class.php');


class ComMainForm extends QForm {

    //Local instance of mct
    protected $mctOrg;
    //Local istance of data obj
    protected $objOrg;

    //panel Space Holders
    protected $pnlComList;
    protected $pnlComEdit;
    protected $pnlComInfo;
    protected $pnlComNoteEdit;
    protected $pnlComNoteList;
    protected $pnlComNoteInfo;

    
    protected $authSys;
    //Controls
    protected $txtAOrg;
    protected $txtAOracleLink;
    protected $lstSeason;
    protected $btnGetList;
    //Tab
    public $tabMain;

    //*****************Events
    //{
    public function txtAOrg_Change($strParameter) {
        return $this->authSys->AutoList_MySchoolList($strParameter, "Name");
    }

    public function txtAOracleLink_Change($strParameter) {
        return $this->authSys->AutoList_MySchoolList($strParameter, "OracleLink");
    }

    public function btnGetList_Click() {

        if ( $this->txtAOrg->Text) {
            $this->objOrg        = Org::LoadByName($this->txtAOrg->Text);
            $_SESSION['OrgId']= $this->objOrg->Id;
            $addon = "/" . $this->objOrg->Id;
        }
        elseif ( $this->txtAOracleLink->Text ) {
            $this->objOrg       = Org::LoadByOracleLink( $this->txtAOracleLink->Text );
            $_SESSION['OrgId']= $this->objOrg->Id;
            $addon = "/" . $this->objOrg->Id;
        }
        elseif ( isset($_SESSION['OrgId']) ) {
            $this->objOrg       = Org::Load($_SESSION['OrgId']);
            $addon = "/" . $this->objOrg->Id;
        }else
            $addon = "";

        $this->txtAOrg->Text        = "";
        $this->txtAOracleLink->Text = "";
        if (!$addon =="") {

            if ( $this->authSys->Check_Org_Perm(  $_SESSION['OrgId'], false) == false ) {
                QApplication::ExecuteJavaScript( 'alert("Cant access this Org");' );
                unset($_SESSION['OrgId']);
                unset($this->objOrg);
                $this->CloseAllPane();
                $this->ClosePanel("pnlComList");
            }
            else {
                $this->pnlComList->Visible = true;
                $this->pnlComList->RemoveChildControls(true);
                $this->pnlComList = new ComSchoolListPanel($this->pnlComList,'SetEditPane', 'ClosePanel');


            }
        }
    }


    //}
    //End of Events
    protected function Form_Run() {
        $this->authSys = new Perm();
        $this->authSys->IsAuthenticated(true);
    }

    protected function Form_Create() {
        //{
        //
        $this->mctOrg = OrgMetaControl::CreateFromPathInfo($this);
        $this->objOrg = $this->mctOrg->__get("Org");

        $this->txtAOracleLink = $this->mctOrg->txtAOracleLink_Create();
        $this->txtAOrg        = $this->mctOrg->txtAOrg_Create();

        $this->btnGetList = new QButton($this);
        $this->btnGetList->Text = 'Get School';
        $this->btnGetList->AddAction(new QClickEvent(500), new QServerAction('btnGetList_Click'));

        $this->lstSeason      = new  QListBox($this);
        $this->lstSeason->Name = "Season";
        $this->lstSeason->AddItem('- All Seasons -', null);

        //Create Tabs and Panels
        $this->tabMain = new JTab("MainTab");

        $this->pnlComList = new QePanel($this);
        $this->pnlComList->AutoRenderChildren = true;
        //tab List All
        $this->tabMain->AddTab(QApplication::Translate("List All Communications") , $this->pnlComList);
        //tab School Com
        $this->tabMain->AddTab(QApplication::Translate("School Communications"));

        $this->pnlComEdit = new QePanel($this);
        $this->pnlComEdit->AutoRenderChildren = true;
        //tab Edit
        $this->tabMain->AddTab(QApplication::Translate("Edit School Communications") , $this->pnlComEdit, QApplication::Translate("School Communications"));

        $this->tabMain->AddTab(QApplication::Translate("Notes") ,null, QApplication::Translate("School Communications"));

        $this->pnlComNoteList = new QePanel($this);
        $this->pnlComNoteList->AutoRenderChildren = true;
        //tab
        $this->tabMain->AddTab( QApplication::Translate("List Notes") , $this->pnlComNoteList,QApplication::Translate("Notes") );

        $this->pnlComNoteEdit = new QePanel($this);
        $this->pnlComNoteEdit->AutoRenderChildren = true;
        //tab
        $this->tabMain->AddTab( QApplication::Translate("Edit Note") , $this->pnlComNoteEdit ,QApplication::Translate("Notes") );

        $this->pnlComNoteInfo = new QePanel($this);
        $this->pnlComNoteInfo->AutoRenderChildren = true;
        //tab
        $this->tabMain->AddTab(QApplication::Translate("Records Info"), $this->pnlComNoteInfo, QApplication::Translate("Notes") );

        $this->pnlComInfo = new QePanel($this);
        $this->pnlComInfo->AutoRenderChildren = true;
        //tab
        $this->tabMain->AddTab(QApplication::Translate("Records Info"), $this->pnlComInfo, QApplication::Translate("School Communications"));




        //tab
        //$this->tabMain->AddTab(QApplication::Translate("Samples") , null, QApplication::Translate("School Communications"));

    }
    public function CloseAllPane($RefreshPanel=null) {
        $this->AutoClosePane('pnlComEdit');
        $this->AutoClosePane('pnlComInfo');

        $this->AutoClosePane('pnlComNoteEdit');
        $this->AutoClosePane('pnlComNoteList');
        $this->AutoClosePane('pnlComNoteInfo');


        //Refresh main panel
        if ($RefreshPanel)
            $this->pnlComList->Refresh();

        $this->tabMain->ChangeTabById(0);
    }

    private function AutoClosePane($objParent) {
        //Close the Edit Pane
        $this->$objParent->RemoveChildControls(true);
        $this->$objParent->Visible = false;
    }

    public function ClosePanel($ClosePanel, $RefreshPanel=null) {
        $this->$ClosePanel->RemoveChildControls(true);
        $this->$ClosePanel->Visible = false;

        if ($RefreshPanel)
            $this->$RefreshPanel->Refresh();
    }

    public function SetEditPane($objPanel , $objParent) {
        $this->$objParent->RemoveChildControls(true);
        if ($objPanel) {
            $objPanel->SetParentControl($this->$objParent);
            $this->$objParent->Visible = true;
        } else {
            $this->$objParent->Visible = false;
        }
        //$this->tabMain->ChangeTabById(1);
    }
    protected function Form_Validate() {
        // By default, we report that Custom Validations passed
        $blnToReturn = true;

        $blnFocused = false;
        /*                foreach ($this->GetErrorControls() as $objControl) {

                        // Set Focus to the top-most invalid control
                        if (!$blnFocused) {
                                $objControl->Focus();
                                $blnFocused = true;
                                $this->tabMain->ActiveTab =  $objControl->ParentControl->__get("CurrentSection") -1;
                        }
                        $objControl->Blink();
                }*/


        return $blnToReturn;
    }
    public function strTruncate($string, $length) {
        $output="";
        settype($string, 'string');
        settype($length, 'integer');
        for($a = 0; $a < $length AND $a < strlen($string); $a++) {
            $output .= $string[$a];
        }
        return($output);
    }
}

ComMainForm::Run('ComMainForm','tpl/com_main.tpl.php');

?>
